<?php
//省份批次数据爬虫

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/bootstrap/database.php';
require_once __DIR__.'/bootstrap/arguments.php';

use App\Services\ProvinceBatchSpiderService;
use App\Workers\ProvinceBatchSpiderWorker;
use Workerman\Worker;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// 解析命令行参数
$args = parseArguments($argv);
$options = $args['options'];
$batchSize = $args['batchSize'];
$limit = $args['limit'];
$startTime = $args['startTime'];

// 创建Worker
$worker = new Worker('http://0.0.0.0:2345');
$worker->name = 'ProvinceLineSpider';
$worker->count = 1;
$worker->reloadable = true;
$worker->user = '';
$worker->stdoutFile = '/tmp/province-batch-spider.stdout.log';
$worker->pidFile = '/tmp/province-batch-spider.pid';
$worker->logFile = '/tmp/province-batch-spider.log';

// 确保logs目录存在
$logDir = dirname(__DIR__).'/college/storage/logs';
if (!is_dir($logDir)) {
    mkdir($logDir, 0755, true);
}

// 初始化日志服务
$logger = new Logger('province-batch-spider');
$logger->pushHandler(new StreamHandler($logDir.'/province-batch-spider.log'));

// 初始化爬虫服务
$proxy_enable = isset($options['proxy_enable']);
$service = new ProvinceBatchSpiderService($proxy_enable);

// $service
ProvinceBatchSpiderWorker::run($worker, $service, $limit, $options);

// 运行worker
Worker::runAll();
